package com.gzhryc.shared_device.oem.h5.mng.controllers.business;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.business.dao.db.CouponActivity;
import com.gzhryc.shared_device.oem.code.business.dao.db.CouponActivityPlace;
import com.gzhryc.shared_device.oem.code.business.dao.enums.CouponActivityPlaceEnum;
import com.gzhryc.shared_device.oem.code.business.services.CouponActivityPlaceService;
import com.gzhryc.shared_device.oem.code.business.services.CouponActivityService;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;

public class CouponActivityPlaceController {

    @Route(value = "/business/coupon_activity/place/list.data")
    public ListResult<CouponActivityPlace> placeListData(HttpServletRequest request) {
        ListResult<CouponActivityPlace> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        Long activityId = WebUtils.getLong(request,"activityId");
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(CouponActivityPlaceService.self().countJoinByActivityId(activityId));
        result.getBody().setData(CouponActivityPlaceService.self().findJoinByActivityId(activityId, pageNo, pageLimit));

        if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
            result.setCode(DtoConstants.SUCCESS,null);
        }
        return result;
    }

    @Route(value = { "/business/coupon_activity/place/add.html" })
    public String list(HttpServletRequest request) {
        Long activityId = WebUtils.getLong(request,"activityId");
        CouponActivity couponActivity = CouponActivityService.self().getJoinById(activityId);
        WebUtils.setAttribute(request,couponActivity);
        return "/business/coupon_activity/place.html";
    }

    @Route(value = { "/business/coupon_activity/place/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"设置失败，请稍后再试");
        try {
            Long activityId = WebUtils.getLong(request,"activityId");
            String ids = WebUtils.getString(request,"ids");
            if(StringTools.isNotBlank(ids)){
                String[] placeIds = ids.split(",");

                if(CouponActivityPlaceService.self().addPlaceIds(activityId,placeIds)){
                    result.setCode(DtoConstants.SUCCESS,null);
                }
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/business/coupon_activity/place/open.do" })
    public AjaxResult doOpen(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"开启失败，请稍后再试");
        Long activityId = WebUtils.getLong(request,"activityId");
        Long placeId = WebUtils.getLong(request,"placeId");
        try {
            if(CouponActivityPlaceService.self().updateState(activityId,placeId, CouponActivityPlaceEnum.State.Enable.index())){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/business/coupon_activity/place/close.do" })
    public AjaxResult doClose(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"关闭失败，请稍后再试");
        Long activityId = WebUtils.getLong(request,"activityId");
        Long placeId = WebUtils.getLong(request,"placeId");
        try {
            if(CouponActivityPlaceService.self().updateState(activityId,placeId, CouponActivityPlaceEnum.State.Disable.index())){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/business/coupon_activity/place/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败，请稍后再试");
        Long activityId = WebUtils.getLong(request,"activityId");
        Long placeId = WebUtils.getLong(request,"placeId");
        try {
            if(CouponActivityPlaceService.self().deleteByKeys(activityId,placeId)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
